import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.StringTokenizer;


public class SourceCode {

	String sourceCode = "";
	ArrayList<Account> accounts = new ArrayList<Account>();
	
	public SourceCode (URLConnection con, int minLine, int maxLine) throws UnsupportedEncodingException, IOException
	{
		int lineCounter = 0;
		String sourceCode  = "";
		// if page does not exist or access is not granted exceptions are thrown here
		Reader r = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
		// Page is valid and accessible
		
		// threadListComplete.add(threadList.get(j));
		StreamTokenizer st = new StreamTokenizer(r);

		st.slashSlashComments(true);
		st.slashStarComments(true);
		st.ordinaryChars(-1, 128);

		while (st.nextToken() != StreamTokenizer.TT_EOF
				&& lineCounter < maxLine) {
			if (st.ttype == StreamTokenizer.TT_EOL) {
				lineCounter++;
			}
			// can't use TT_WORD or TT_NUMBER here
			if (lineCounter > minLine
					&& lineCounter < maxLine) {
				sourceCode += (char) st.ttype;
			}
		}
		r.close();
		this.sourceCode =  sourceCode;
	}
	
	public String getSourceCode() {
		return this.sourceCode;
	}
	public void setSourceCode(String sourceCode) {
		this.sourceCode = sourceCode;
	}
	public ArrayList<Account> getAccounts() {
		return this.accounts;
	}
	public void setAccounts(ArrayList<Account> accounts) {
		this.accounts = accounts;
	}
	
	
	public ArrayList<Account> scrapeDataFromSource(String sourceCode) {
		
		ArrayList<Account> result = new ArrayList<Account>();
		String scrapeData = sourceCode;
		
		// make some scraping and formatting -- HAVE TO IMPROVE THIS!
		scrapeData.trim();
		scrapeData.replaceAll("\t", "");
		scrapeData.replaceAll("\n", "");
		scrapeData.replaceAll("\r", "");
		
		StringTokenizer filter = new StringTokenizer(scrapeData);
		while (filter.hasMoreTokens()) {
			String dummy = filter.nextToken();
			
			// TODO: create blacklist object containing all items
			if (dummy.contains("<span>") && !(dummy.contains("<img"))
					&& !(dummy.contains("Themenstarter"))
					&& !(dummy.contains("postTopic"))
					&& !(dummy.contains("Werbung"))
					&& !(dummy.contains("Mein"))
					&& !(dummy.contains("Ungelesen"))
					&& !(dummy.contains("Unbeantwortete"))
					&& !(dummy.contains("Themen"))
					&& !(dummy.contains("Alle"))
					&& !(dummy.contains("Forum"))
					&& !(dummy.contains("OverHill"))
					&& !(dummy.contains("Off-Topic"))
					&& !(dummy.contains("Archiv"))
					&& !(dummy.contains("class="))) {
				dummy = dummy.replaceAll("<span>", "").replaceAll(
						"</span>", "");

				if (!(result.contains(dummy))) {
					Account newAccount = new Account(dummy, null);
					result.add(newAccount);
					System.out.println("User: " + dummy);
				}
			}
		}
			
		return result;
	}
	
}
